package com.darthsith.droidathome.core.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.darthsith.droidathome.core.dao.DeviceDao;
import com.darthsith.droidathome.core.model.Device;

@Repository
public class DeviceDaoImpl implements DeviceDao {

	@Autowired
	private SessionFactory sessionFactory;
	
	@Override
	public void addDevice(Device device) {
		sessionFactory.getCurrentSession().save(device);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Device> listAll() {
		return sessionFactory.getCurrentSession().createQuery("FROM DEVICE").list();
	}

	@Override
	public void removeDevice(Device device) {
		String macAddress=device.getMacAddress();
		Device d=(Device) sessionFactory.getCurrentSession().load(Device.class, macAddress);
		if (null!=d)
			sessionFactory.getCurrentSession().delete(d);
	}
	
	@Override
	public Device getDevice(String macAddress) {
		return (Device) sessionFactory.getCurrentSession().load(Device.class, macAddress);
	}

}
